import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { isArray } from '/@/utils/is';
export enum DataType {
  FLOW = 'com.redmoon.oa.job.WorkflowJob',
  SCRIPT = 'com.redmoon.oa.job.BeanShellScriptJob',
  SUPSCRIBE = 'com.redmoon.oa.job.SynThirdPartyDataJob',
}

export const columns: BasicColumn[] = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 80,
    ellipsis: true,
    resizable: true,
  },
  {
    title: '名称',
    dataIndex: 'jobName',
    width: 150,
    ellipsis: true,
    resizable: true,
  },
  {
    title: '类型',
    dataIndex: 'jobName',
    width: 100,
    ellipsis: true,
    resizable: true,
    ifShow: false,
  },
  {
    title: '类名称',
    dataIndex: 'jobClass',
    width: 200,
    ellipsis: true,
    resizable: true,
  },
  {
    title: '调度',
    dataIndex: 'cron',
    width: 150,
    ellipsis: true,
    resizable: true,
  },
  {
    title: '用户',
    dataIndex: 'realName',
    width: 100,
    ellipsis: true,
    resizable: true,
  },
];

export const searchFormSchema: FormSchema[] = [];

export const formSchema: FormSchema[] = [
  {
    field: 'formCode',
    label: '表单',
    required: true,
    component: 'Input',
    colProps: { span: 24 },
    slot: 'formCode',
  },
  {
    field: 'job_name',
    label: '名称',
    required: true,
    component: 'Input',
    colProps: { span: 24 },
  },
  {
    field: 'kind',
    label: '类型',
    required: true,
    component: 'RadioButtonGroup',
    colProps: { span: 24 },
    defaultValue: 0,
    componentProps: {
      options: [
        { value: 0, label: '按日期' },
        { value: 1, label: '按星期' },
        { value: 2, label: '按间隔' },
        { value: 3, label: '按cron表达式' },
      ],
    },
  },
  {
    field: 'type_1',
    label: '开始时间',
    component: 'Input',
    colProps: { span: 24 },
    slot: 'type_1',
    ifShow: ({ values }) => values.kind == '0',
  },
  {
    field: 'type_2',
    label: '开始时间',
    component: 'Input',
    colProps: { span: 24 },
    slot: 'type_2',
    ifShow: ({ values }) => values.kind == '1',
  },
  {
    field: 'type_3',
    label: '每隔',
    component: 'Input',
    colProps: { span: 24 },
    slot: 'type_3',
    ifShow: ({ values }) => values.kind == '2',
  },
  {
    field: 'mycron',
    label: 'cron表达式',
    required: true,
    component: 'Input',
    colProps: { span: 24 },
    slot: 'mycron',
    ifShow: ({ values }) => values.kind == '3',
  },
  {
    field: 'flowCode',
    label: '选择流程',
    required: true,
    helpMessage: '只能选择能确认发起人或者发起角色的流程',
    component: 'Select',
    colProps: { span: 24 },
    slot: 'flowCode',
  },
];

export function setChildren(node, bool = true) {
  node.forEach((item) => {
    item.key = item.code;
    item.value = item.code;
    item.title = item.name;
    if (bool) {
      item.disabled = item.layer == 3 ? false : true;
    }
    if (item.children && Array.isArray(item.children) && item.children.length > 0) {
      item.isLeaf = false;
      setChildren(item.children);
    } else {
      item.isLeaf = true;
    }
  });
  return node;
}

export const weekDayOptions = [
  {
    label: '星期日',
    value: '1',
  },
  {
    label: '星期一',
    value: '2',
  },
  {
    label: '星期二',
    value: '3',
  },
  {
    label: '星期三',
    value: '4',
  },
  {
    label: '星期四',
    value: '5',
  },
  {
    label: '星期五',
    value: '6',
  },
  {
    label: '星期六',
    value: '7',
  },
];

export const intervalTypeOptions = [
  {
    label: '天',
    value: 'd',
  },
  {
    label: '时',
    value: 'h',
  },
  {
    label: '分',
    value: 'm',
  },
  {
    label: '秒',
    value: 's',
  },
];

export function getData(data, type) {
  console.log('data', data);
  const obj: Recordable = {};
  // const cron = data.cron;
  // const ary = cron.split(' ');
  // console.log('ary', ary);
  // if (ary[0] && ary[0].length == 1) ary[0] = '0' + ary[0];
  // if (ary[1] && ary[1].length == 1) ary[1] = '0' + ary[1];
  // if (ary[2] && ary[2].length == 1) ary[2] = '0' + ary[2];
  // let interval = '';
  // let intervalType = '';
  // let p = ary[0] ? ary[0].indexOf('/') : -1;
  // if (p != -1) {
  //   interval = ary[0].substring(p + 1);
  //   intervalType = 's';
  // }
  // p = ary[1] ? ary[1].indexOf('/') : -1;
  // if (p != -1) {
  //   interval = ary[1].substring(p + 1);
  //   intervalType = 'm';
  // }
  // p = ary[2] ? ary[2].indexOf('/') : -1;
  // if (p != -1) {
  //   interval = ary[2].substring(p + 1);
  //   intervalType = 'h';
  // }
  // p = ary[3] ? ary[3].indexOf('/') : -1;
  // if (p != -1) {
  //   interval = ary[3].substring(p + 1);
  //   intervalType = 'd';
  // }
  // let t = '';
  // if (!intervalType) {
  //   t = ary[2] + ':' + ary[1] + ':' + ary[0];
  // }
  let data_map = '';
  if (type === DataType.SUPSCRIBE) {
    if (data.dataMap) {
      const dataMap = JSON.parse(data.dataMap);
      console.log('dataMap', dataMap);
    }
    obj.tables = data.table;
  } else if (type === DataType.SCRIPT) {
    data_map = data.dataMap;
  }
  const newData = {
    ...data,
    ...obj,
    // interval,
    // intervalType,
    // time: t,
    job_name: data.jobName,
    month_day: data.monthDay,
    data_map,
    mycron: data.cron,
    job_class: data.jobClass,
    user_name: data.realName,
  };
  return newData;
}

export function setData(data, type) {
  const obj = { formData: {}, allow: false, message: '' };
  const formData = {} as any; //new FormData();
  if (type === DataType.FLOW) {
    // formData.append('data_map', data.flowCode);
    formData.data_map = data.flowCode;
  } else if (type === DataType.SUPSCRIBE) {
    const data_map =
      '{"formCode":"' +
      data.formCode +
      '", "dbSource":"' +
      data.dbSource +
      '", "table":"' +
      data.tables +
      '", "primaryKey":"' +
      data.primaryKey +
      '", "foreignKey":"' +
      data.foreignKey +
      '"}';
    // formData.append('data_map', data_map);
    formData.data_map = data_map;
  } else if (type === DataType.SCRIPT) {
    // formData.append('data_map', data.data_map);
    formData.data_map = data.data_map;
  }
  const t = data.time;
  const ary = t.split(':');
  if (ary[2] && ary[2].indexOf('0') == 0 && ary[2].length > 1)
    ary[2] = ary[2].substring(1, ary[2].length);
  if (ary[1] && ary[1].indexOf('0') == 0 && ary[1].length > 1)
    ary[1] = ary[1].substring(1, ary[1].length);
  if (ary[0] && ary[0].indexOf('0') == 0 && ary[0].length > 1)
    ary[0] = ary[0].substring(1, ary[0].length);
  let weekDay = '';
  if (isArray(data.weekDay) && data.weekDay.length) {
    weekDay = data.weekDay.join(',');
  }

  // { value: 0, label: '按日期' }, time month_day
  // { value: 1, label: '按星期' },time weekDay
  // { value: 2, label: '按间隔' }, intervalType interval
  // { value: 3, label: '按cron表达式' },
  let dayOfMonth = data.month_day;
  const kind = data.kind;
  if (kind == '0') {
    if (dayOfMonth == '') {
      obj.allow = true;
      obj.message = '请输入每月几号！';
      return obj;
    }
    if (dayOfMonth != '' && (parseInt(dayOfMonth) > 31 || parseInt(dayOfMonth) < 0)) {
      obj.allow = true;
      obj.message = '每月天数不能小于0，且不能大于31';
      return obj;
    }
    if (dayOfMonth == '') dayOfMonth = '?';
    const cron = ary[2] + ' ' + ary[1] + ' ' + ary[0] + ' ' + dayOfMonth + ' * ?';
    // formData.append('cron', cron);
    formData.cron = cron;
    formData.interval = '';
    formData.intervalType = '';
  } else if (kind == '1') {
    if (weekDay == '') {
      obj.allow = true;
      obj.message = '请选择星期几！';
      return obj;
    }
    if (dayOfMonth == '') dayOfMonth = '?';
    const cron = ary[2] + ' ' + ary[1] + ' ' + ary[0] + ' ? * ' + weekDay;
    // formData.append('cron', cron);
    formData.cron = cron;
    formData.interval = '';
    formData.intervalType = '';
    formData.month_day = '';
  } else if (kind == '2') {
    const intervalType = data.intervalType;
    if (!intervalType) {
      obj.allow = true;
      obj.message = '请选择天、时、分或秒！';
      return obj;
    }
    const val = data.interval;
    if (!val) {
      obj.allow = true;
      obj.message = '请输入时间间隔！';
      return obj;
    }
    let cron = '';
    if (intervalType == 's') {
      cron = '*/' + val + ' * * * * ?';
    } else if (intervalType == 'm') {
      cron = '0 */' + val + ' * * * ?';
    } else if (intervalType == 'h') {
      cron = '0 0 */' + val + ' * * ?';
    } else if (intervalType == 'd') {
      cron = '0 0 0 */' + val + ' * ?';
    }
    // formData.append('cron', cron);
    formData.cron = cron;
    formData.month_day = '';
  } else if (kind == '3') {
    if (data.mycron == '') {
      obj.allow = true;
      obj.message = '请输入cron表达式！';
      return obj;
    }
    // o('cron').value = o('mycron').value;
    // formData.append('cron', data.mycron);
    formData.cron = data.mycron;
    formData.interval = '';
    formData.intervalType = '';
    formData.month_day = '';
  }
  obj.formData = {
    ...data,
    ...formData,
    weekDay,
    // job_name: data.jobName,
    // kind: data.kind,
    // time: data.time,
    // month_day: data.monthDay,
    // interval: data.interval,
    // mycron: data.mycron,
    // id: data.id,
    // job_class: data.jobName,
    // cron: data.cron,
    // data_map: data.dataMap,
    // user_name: data.userName,
    // ...formData,
  };
  return obj;
}
